package com.google.code.qsecurity.client.mvp;

import com.google.code.qsecurity.client.event.OfflineEvent;
import com.google.code.qsecurity.client.event.OnlineEvent;
import com.google.code.qsecurity.client.event.StateChangeEvent;
import com.google.code.qsecurity.client.event.StateChangeHandler;
import com.google.code.qsecurity.shared.proxy.UserProxy;
import com.google.web.bindery.event.shared.EventBus;

public class StateController {

	public enum State {
		OFFLINE, ONLINE
	}

	private UserProxy user;
	private State state;
	private final EventBus eventBus;

	public StateController(EventBus eventBus) {
		this.eventBus = eventBus;
		state = State.OFFLINE;
		register();
	}
	
	private void register() {
		eventBus.addHandler(OnlineEvent.getType(), new StateChangeHandler() {
			
			@Override
			public void onStateChange(StateChangeEvent event) {
				user = event.getUser();
				state = State.ONLINE;
			}
			
		});

		eventBus.addHandler(OfflineEvent.getType(), new StateChangeHandler() {
			
			@Override
			public void onStateChange(StateChangeEvent event) {
				user = event.getUser();
				state = State.OFFLINE;
			}
			
		});
	}
	
	public UserProxy getUser() {
		return user;
	}
	
	public State getState() {
		return state;
	}
	
}
